package com.wanshi.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanshi.cloud.client.ProductsClient;
import com.wanshi.cloud.entity.OrderItem;
import com.wanshi.cloud.entity.Orders;
import com.wanshi.cloud.entity.Products;
import com.wanshi.cloud.mapper.OrdersMapper;
import com.wanshi.cloud.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-09-03
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private ProductsClient productsClient;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Orders findbyid(String id) {
//        Orders orders = this.getById(id);

        Orders orders=ordersMapper.findbyid(id);

        //根据订单id查询数量

        List<OrderItem> items = orders.getItems();


        for(OrderItem item :items){

            Products prod = productsClient.findbuid(item.getProdId() + "");

            item.setProducts(prod);
        }
        return orders;


    }

    @Override
    public List<Orders> findbyuserid(String userid) {

        QueryWrapper wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",userid);

        List <Orders>list = this.list(wrapper);
        return list;
    }
}
